const Stock = require("../../modules/Stock")
const $asyncForeach = require("../../utils/asyncForeach")

const list = [
    { code: "000001", type: "index", market_type: "ab" },
    { code: "HZ2083", type: "index", market_type: "hk" },
    { code: "NDX", type: "index", market_type: "us" }
]

module.exports = async () => {
    let result = []

    result = await $asyncForeach({
        list, fn: async ({ code, type }, index) => {

            let result = {}
            let stock = new Stock({ code, type })
            let transaction = await stock.getTransaction({ ktype: "day" })
            let details = await stock.generateDetails({ transaction })
            let detailsLatest = Stock.getDetailsLatest({ details })

            let divergence = Stock.divergence({ details: detailsLatest })
            let latestKd = Stock.latestKd({ details: detailsLatest })
            let latestMaDifference = Stock.latestMaDifference({ details: detailsLatest })

            detailsLatest.analysis = { divergence, latestKd, latestMaDifference }

            delete detailsLatest.calculate.macd.dea
            delete detailsLatest.calculate.macd.diff
            delete detailsLatest.calculate.kd.d

            result = detailsLatest

            return result
        }
    })

    return result
}